#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)//初始化棋盘
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
void SetMine(char board[ROWS][COLS], int row, int col)//设置雷
{
	int count = MINE;
	while (count)
	{
		int x = rand() % row + 1;//1~9
		int y = rand() % col + 1;//1~9
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)//展示棋盘
{
	printf("--------------扫雷-------------\n");
	for (int i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i < row + 1; i++)
	{
		printf("%d ", i);
		for (int j = 1; j < col + 1; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

int GetMine(char mine[ROWS][COLS],int x, int y)//FindMine中的得到旁边雷的个数
{
	int t = 0;
	for (int i = x-1; i <=x+1; i++)
	{
		for (int j =y-1; j <= y+1; j++)
		{
			t += mine[i][j] - '0';
		}
	}
	return t;
}

void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)//找雷
{
	int x=0;
	int y=0;
	int count = MINE;
	while (row*col-count)
	{
		printf("请输入你要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("boom~泥被炸4惹!\n");
				break;
			}
			else
			{
				int t = GetMine(mine, x, y);
				show[x][y] = t + '0';
				count++;
				DisplayBoard(show, ROW, COL);//输入完之后再打印棋盘
			}
			if (row * col - count == 0)
			{
				printf("恭喜泥挑战成功喵!!\n");
				break;
			}
		}
		else
		{
			printf("输入错误，请重新输入:\n");
		}
	}
}
